package com.lanren.familybookapp.ui.table.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.table.bean.InOutStatisticsBean;
import com.lanren.familybookapp.ui.table.bean.TotalStaticsBean;
import com.lanren.familybookapp.ui.table.interfaces.ITableModel;

import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName MainModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/5 16:22
 * @Version 1.0
 */
public class TableModel extends BaseModel {

    public TableModel() {
        initDispoable();
    }

    public void getTotal(int bookId, String bookTime, final ITableModel.TableTotalModel totalModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        map.put("bookTime", bookTime);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_BOOKKEEPING_GET_TOTAL)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<TotalStaticsBean>() {
                    @Override
                    public void onError(ApiException e) {
                        totalModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(TotalStaticsBean totalStaticsBean) {
                        totalModel.onSuccess(totalStaticsBean);
                    }

                });
        addDisposable(disposable);
    }

    public void getStatistics(int bookId, String bookTime, int inOut, final ITableModel.TableStaticsModel staticsModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        map.put("bookTime", bookTime);
        map.put("inOut", inOut + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_BOOKKEEPING_GET_STATISTICS)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<InOutStatisticsBean>() {
                    @Override
                    public void onError(ApiException e) {
                        staticsModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(InOutStatisticsBean inOutStatisticsBean) {
                        staticsModel.onSuccess(inOutStatisticsBean);

                    }

                });
        addDisposable(disposable);
    }
}
